package com.cwt_taxi_xa.protocol.card.cmd;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @param <T> 回调
 * @author undi
 */

public abstract class BaseCmd<T extends AckCallBack> {
	protected String cmdTag;
	protected T ackCallBack;

	protected boolean arrayIsEmpty = false;

	protected byte[] cacheData;
	//对于超过300字节的数据读取,建议260字节为单位
	protected int dataLength = 260;//测试60个字节是没有问题的

	public BaseCmd(String cmdTag) {
		this.cmdTag = cmdTag;
	}



	public BaseCmd(String cmdTag, T ackCallBack) {
		this.cmdTag = cmdTag;
		this.ackCallBack = ackCallBack;
	}



	public String getTAG(){
		return cmdTag;
	}

	public abstract void decode(byte[] buffer);

	public abstract String data();

	public int cmd(){
		return 0x0021;
	};


	public void statSend(){
		if(ackCallBack != null){
			ackCallBack.sendCmd(cmd(), data());
		}
	}

	protected void saveCacheData(byte[] source){
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream();
			if(cacheData != null){
				baos.write(cacheData);
				baos.write(source);
			}else{
				baos.write(source);
			}
			cacheData = baos.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	protected boolean defaultNumber(String number){
		if(number != null && (number.contains("AAAAAA") || number.equals("AAAAAA"))){
			return true;
		}
		return false;
	}

	/*
	 * 大端模式
	 * 低地址存放高位字节
	 */
	public static int bigBytes2Int(byte[] bytes) {
		int num = bytes[1] & 0xFF;
		num |= ((bytes[0] << 8) & 0xFF00);
		return num;
	}

	/*
	 * 小端模式
	 */
	public static int smallBytes2Int(byte[] bytes) {
		int num = bytes[0] & 0xFF;
		num |= ((bytes[1] << 8) & 0xFF00);
		return num;
	}

	/**
	 *
	 * @param buffer
	 * @param start 开始位置
	 * @param length 截取长度
	 * @return  返回截取的buffer
	 */
	public static byte[] byteToSubstring(byte[] buffer,int start,int length){
		byte[] result = new byte[length];
		if(buffer != null){
			System.arraycopy(buffer, start, result, 0, length);
		}
		return result;
	}

	/**
	 * 获取最后不为空的索引
	 * @param buffer
	 * @return
	 */
	public static int lastIndexOfNull(byte[] buffer){
		int index = -1;
		for(int i =  buffer.length -1; i>0 ; i--){
			if(buffer[i] != 0x00){
				index = i;
				break;
			}
		}
		return index;
	}

	public static boolean lastIndexIsNull(byte[] buffer){
		boolean result = false;
		if(buffer != null){
			if(buffer[buffer.length-1] == 0x00){
				result = true;
			}
		}
		return result;
	}


	public static boolean arrayIsNull(byte[] buffer){
		boolean result = true;
		for(int i =0 ; i < buffer.length; i++){
			//从头判断对于图片效果较好
			if(buffer[i] != 0x00){
				result = false;
				break;
			}
		}
		return result;
	}


	/**
	 * 保留一位空字节
	 * @param buffer
	 * @return
	 */
	public static byte[] substringSecondLastNull(byte[] buffer){
		int index = lastIndexOfNull(buffer);
		if(index > 0){
			index +=1;
			if(index < buffer.length -1){
				return byteToSubstring(buffer,0,index);
			}
		}
		return buffer;
	}

	protected void saveFile(String path,byte[] fileData){
		File file = new File(path);
		if(file != null && file.exists()){
			file.delete();
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		ByteArrayInputStream inStream = null;
		FileOutputStream fs = null;
		try {
			inStream = new ByteArrayInputStream(fileData);
			fs = new FileOutputStream(file);

			byte[] buffer = new byte[1024];
			int byteread = 0;
			while ( (byteread = inStream.read(buffer)) != -1) {
				fs.write(buffer, 0, byteread);
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(fs != null){
				try {
					fs.flush();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					fs.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if(inStream != null){
				try {
					inStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}
